<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Repositories\Shop\WxShopOrderAfter;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxShopOrderAfterController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $admin_user = Admin::user();
        return Grid::make(new WxShopOrderAfter(['user']), function (Grid $grid) use ($admin_user){
            $grid->simplePaginate();
            if(!$admin_user->isAdministrator()){
                if(_empty_($admin_user->uid)){
                    $grid->model()->where('id', '<', -1);
                }else{
                    $grid->model()->where('seller_user_id', $admin_user->uid);
                }
            }
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'title', 'content'])->placeholder('搜索订单售后ID，标题，内容...');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user.user_avatar','头像')->image('',30,30);
            $grid->column('user.user_name','用户');
            $grid->column('title')->label('primary');
            $grid->column('content')->limit(20);
            $grid->column('order_id');
//            $grid->column('user_id');
            $grid->column('state')->using([0 => '未处理', 1 => '已处理', 2 => '拒绝'])->label([
                0 => 'red',
                1 => 'green',
                2 => 'orange2',
            ]);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
            $grid->disableActions();
            $grid->disableBatchActions();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->header(function ($collection) {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block;margin-top: 20px;">
小提示: “催发货”的售后需要你发货才能处理，“退款申请”需要你退款了才能处理</div>';
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopOrderAfter(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('order_id');
            $show->field('user_id');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShopOrderAfter(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('content');
            $form->text('order_id');
            $form->text('user_id');
            $form->switch('state')->help('开启为已处理');
            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });
        });
    }
}
